package sketch.ink;

import java.util.ArrayList;
import java.util.List;

import sketch.doc.Sketch;
import sketch.doc.SketchContainer;

public class StrokeContainer {

	public static boolean DO_NORMALIZATION = false;

	public static double MIN_GAP = 5;

	public static double MAX_GAP = 10;

	public static List<SampledPoint> normalize(List<SampledPoint> ps) {
		if (!DO_NORMALIZATION)
			return ps;

		List<SampledPoint> newPs = new ArrayList<SampledPoint>();

		if (ps.size() < 1) {
			return newPs;
		}

		SampledPoint p = ps.remove(0);
		newPs.add(p);
		while (!ps.isEmpty()) {
			SampledPoint pp = ps.get(0);

			double dis = p.toPoint().distance(pp.toPoint());
			if (dis < MIN_GAP) {
				ps.remove(0);
			} else if (dis > MAX_GAP) {
				ps.add(0, new SampledPoint(p.toPoint().linearInterpolate(pp.toPoint(), 0.5)));
			} else {
				p = ps.remove(0);
				newPs.add(p);
			}
		}
		newPs.add(p);

		return newPs;
	}

	private static Stroke instance = null;

	public static Stroke get() {
		if (instance == null)
			instance = new Stroke();
		return instance;
	}

	public static void reset() {
//		instance.getCD().detectCorners();
		instance = null;
	}

	public static Stroke fetch() {
		Stroke s = instance;
		s.setPoints(normalize(s.getPoints()));
		s.setId(Sketch.generateId());
		reset();
		return s;
	}

//	public static Stroke pop() {
//		if (instance == null)
//			return null;
//
//		CornerDetector cd = SketchContainer.getCD();
//		cd.setPoints(instance.getPoints());
//		instance.setCorners(cd.getCorners());
//		return instance;
//	}
}
